/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ohos.speechutilsdemo.slice;

import com.ohos.speechutilsdemo.ResourceTable;

import ee.ioc.phon.ohos.speechutils.abilityslice.AsrBaseAbilitySlice;
import ee.ioc.phon.ohos.speechutils.view.MicButton;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 语音转文字
 *
 * @since 2021-03-20
 */
public class AsrAbilitySlice extends AsrBaseAbilitySlice {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "AsrAbilitySlice");
    private MicButton recorderView;
    private Text text;

    @Override
    public int setLayout() {
        return ResourceTable.Layout_ability_asr;
    }

    @Override
    public void initView() {
        recorderView = (MicButton) findComponentById(ResourceTable.Id_recorder);
        text = (Text) findComponentById(ResourceTable.Id_text);
        recorderView.setTouchEventListener((component, touchEvent) -> {
            switch (touchEvent.getAction()) {
                case TouchEvent.PRIMARY_POINT_DOWN:
                    HiLog.info(LABEL_LOG, "buttonDowm");
                    startRecoding();
                    break;
                case TouchEvent.PRIMARY_POINT_UP:
                    HiLog.info(LABEL_LOG, "buttonUp");
                    stopRecoding();
                    break;
                default:
                    break;
            }
            return false;
        });
    }

    @Override
    public void showRmsChanged(float chage) {
        recorderView.setRmsdbLevel(chage);
    }

    @Override
    public void showAsrResult(String info) {
        text.setText(info);
    }
}
